package com.lwj.liwanjia.view.activity.work.shang_jia;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.WoDeJiLuHomeAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.MySpBean;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.SpfUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/13.
 */

public class WoDeJiLvHomeActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(WoDeJiLvHomeActivity.this, "数据有误", Toast.LENGTH_SHORT).show();
                    break;
                case 1:

                    if (homeList != null && homeList.size() > 0) {
                        mRecyclerView.setVisibility(View.VISIBLE);
                        tvRecord.setVisibility(View.GONE);
                        LinearLayoutManager manager = new LinearLayoutManager(WoDeJiLvHomeActivity.this);
                        mRecyclerView.setLayoutManager(manager);
                        homeAdapter = new WoDeJiLuHomeAdapter(WoDeJiLvHomeActivity.this, homeList);
                        mRecyclerView.setAdapter(homeAdapter);

                    }else{
                        mRecyclerView.setVisibility(View.GONE);
                        tvRecord.setVisibility(View.VISIBLE);
                    }

                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private RecyclerView mRecyclerView;
    private WoDeJiLuHomeAdapter homeAdapter;
    private List<MySpBean.DataBean> homeList;
    private TextView tvRecord;

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        tvRecord= (TextView) findViewById(R.id.tv_record);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("我的记录");

        tv_back.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        //请求网络 获取数据
        initOkHttp();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wo_de_ji_lv_home;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
        }
    }

    //请求网络 获取数据
    private void initOkHttp() {

        String url = URLConstant.BASE_URL + "shop/showGoods";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "我的商品数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "我的商品获取成功");

                try {
                    String result = response.body().string();
                    //Log.d("Tag",result);
                    parseType(result);
                } catch (Exception e) {

                }


            }
        });
    }

    //解析数据
    private void parseType(String result) {

        Gson gson = new Gson();
        MySpBean bean = gson.fromJson(result, MySpBean.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;
                case 1:

                    homeList = new ArrayList<>();

                    for (int i = 0; i < bean.getData().size(); i++) {

                        MySpBean.DataBean dataBean = bean.getData().get(i);
                        homeList.add(dataBean);
                    }

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }
}
